const fs = require('fs');
const args = require('minimist')(process.argv.slice(2));

const helpStr = String.raw`node .\create_file_url.js --dir=FilesDir --out=URLJSONName`;

let dir, out;

if(args['dir']){
   dir = args['dir'];
}else{
  console.log(helpStr);
  process.exit();
}

if(args['out']){
  out = args['out'];
}else{
  console.log(helpStr);
  process.exit();
}

const filenames = fs.readdirSync(dir); 
let json = {};
filenames.forEach(file => { 
  json[file.slice(0, file.lastIndexOf('.'))] = `/${dir}/${file}`; 
}); 

fs.writeFileSync(`${out}`, JSON.stringify(json));

console.log(`写入文件${out}成功.`);